package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.message.presenter.EditGroupInfoPresenter;
import com.beiing.tianshuai.tianshuai.message.view.EditGroupInfoViewImpl;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

public class EditGroupNoticeActivity extends BaseActivity implements EditGroupInfoViewImpl {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnPublish;
    @BindView(R.id.tv_edit_label)
    TextView mTvEditLabel;
    @BindView(R.id.et_new_group_notice)
    EditText mEtNewGroupNotice;

    private Context mContext;
    private String mGroupId;
    private String mGroupDesc;
    private String mGId;
    private EditGroupInfoPresenter mPresenter;
    private String mGroupName;
    private int mType;
    private String mGroupNotice;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_group_notice;
    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        mContext = this;
        initData();
        initToolbar();
        initPresenter();
        initListener();
    }

    private void initData() {
        Intent intent = getIntent();
        mGroupName = intent.getStringExtra("groupName");
        mGroupNotice = intent.getStringExtra("groupNotice");
        mGroupDesc = intent.getStringExtra("groupDesc");
        mGroupId = intent.getStringExtra("groupId");
        mGId = intent.getStringExtra("gId");
        mType = intent.getIntExtra("type", AppConstant.GROUP_DESC);
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setText("保存");
        String title = null;
        switch (mType) {
            case AppConstant.GROUP_NOTICE:
                title = "群公告";
                mEtNewGroupNotice.setHint(mGroupNotice);
                break;
            case AppConstant.GROUP_DESC:
                title = "群简介";
                mEtNewGroupNotice.setHint(mGroupDesc);
                break;
        }
        mTvEditLabel.setText(title);
        mToolbarTvTitle.setText(title);
    }

    private void initPresenter() {
        mPresenter = new EditGroupInfoPresenter(this);
    }

    private void initListener() {
        //保存
        mToolbarBtnPublish.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                switch (mType) {
                    case AppConstant.GROUP_DESC:
                        mPresenter.getEditGroupInfoRequestResponse(
                                UserInfoBean.getUid(mContext),
                                mGId,
                                mGroupId,
                                mGroupName,
                                mEtNewGroupNotice.getText().toString(),
                                mGroupNotice
                        );
                        break;
                    case AppConstant.GROUP_NOTICE:
                        mPresenter.getEditGroupInfoRequestResponse(
                                UserInfoBean.getUid(mContext),
                                mGId,
                                mGroupId,
                                mGroupName,
                                mGroupDesc,
                                mEtNewGroupNotice.getText().toString()
                        );
                        break;
                }
            }
        });

        //返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在提交，请稍后...", false);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(CommonBean bean) {
        if (bean.getCode() == 200) {
            Intent intent = new Intent();
            intent.putExtra("groupNotice", mEtNewGroupNotice.getText().toString());
            this.setResult(AppConstant.EDIT_GROUP_NOTICE, intent);
            this.finish();
        } else {
            showShortText("哎呀，请求出错了...");
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常");
    }
}
